package cn.rkylin.oms.salespromotion.rules.dao.impl.mybatis;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.salespromotion.rules.dao.ISalesPromotionRuleDAO;
import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionRule;
import cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO;

@Repository(value = "rulesDAO")
public class SalesPromotionRuleDAOImpl implements ISalesPromotionRuleDAO {

    public SalesPromotionRuleDAOImpl() {
        
    }
    
    private static final String STMT_INSERT_RULE = "insertRuleSelective";
    private static final String STMT_UPDATE_BY_RULE_ID = "updateRuleByPrimaryKeySelective";
    private static final String SELECT_RULE_STATE_COUNT = "selectRuleStateCount";
    private static final String SELECT_AVAILABLE_RULE = "selectAvailableRule";
    private static final String SELECT_DUPLICATED_RULE = "selectDuplicatedRule";
    
    @Autowired
    protected IDataBaseFactory dao;
    
    @Override
    public int insertSalesPromotionRule(SalesPromotionRule rule) throws Exception {
        int returnValue = dao.insert(STMT_INSERT_RULE, rule);
        return returnValue;
    }

    @Override
    public int updateSalesPromotionRule(SalesPromotionRule rule) throws Exception {
        int returnValue =dao.update(STMT_UPDATE_BY_RULE_ID, rule);
        return returnValue;
    }

    @Override
    public int deleteSalesPromotionRuleById(String ruleId) throws Exception {
        SalesPromotionRule rule = new SalesPromotionRule();
        rule.setRuleId(ruleId);
        rule.setDeleted("y");
        rule.setUpdateTime(new Date());
        int returnValue = dao.delete(STMT_UPDATE_BY_RULE_ID, rule );
        return returnValue;
    }

    @Override
    public List<HashMap> selectStateCount(SalesPromotionRuleVO queryParamVO) throws Exception {
        return dao.find(SELECT_RULE_STATE_COUNT, queryParamVO);
    }

    /**
     * 根据查询条件获取规则列表.
     * @throws Exception 
     * @see cn.rkylin.oms.salespromotion.rules.dao.ISalesPromotionRuleDAO#selectAvailableRule(cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO)
     */
    @Override
    public List<SalesPromotionRuleVO> selectAvailableRule(SalesPromotionRuleVO queryParam) throws Exception {
        return dao.find(SELECT_AVAILABLE_RULE, queryParam);
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.salespromotion.rules.dao.ISalesPromotionRuleDAO#selectDuplicatedRule(cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO)
     */
    @Override
    public List<SalesPromotionRuleVO> selectDuplicatedRule(SalesPromotionRuleVO ruleVO) throws Exception {
        return dao.find(SELECT_DUPLICATED_RULE, ruleVO);
    }

}
